<?php

namespace App;

use App\Model\CartItem;
use App\Model\Order;
use App\Model\Product;
use App\Model\UserAddress;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    public function getAvatarAttribute()
    {
        return \Avatar::create($this->name)->toBase64();
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }
    // 地址
    public function addresses()
    {
        return $this->hasMany(UserAddress::class);
    }

    // 购物车
    public function cartItems()
    {
        return $this->hasMany(CartItem::class);
    }

    // 关注
    public function favoriteProducts()
    {
        return $this->belongsToMany(Product::class,'user_favorite_products')
            ->withTimestamps()->orderBy('user_favorite_products.created_at','desc');
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

}
